package com.jl.security;

import java.security.GeneralSecurityException;

import com.jl.card.ErrorProcess;
import com.jl.card.JL;
import com.jl.security.disperse.DESedeNopadding16;
import com.jl.security.disperse.IDisperse;

public class Disperse {
	
	static{
		Crypto.init();
	}

	public static byte[] disperse(String name, byte[] key, byte[] data) {
		try {
			IDisperse dis = getDisperse(name);
			if (dis != null) {
				dis.initDisData(data);
				return dis.getKey(key);
			} else {
				throw new GeneralSecurityException("Disperse name wrong");
			}
		} catch (GeneralSecurityException e) {
			ErrorProcess.throwRuntimeException(e.getMessage());
		}
		return null;
	}

	private static IDisperse getDisperse(String name) {
		if (name.equalsIgnoreCase(JL.DERIVE_PBOC)) {
			return new DESedeNopadding16();
		}
		return null;
	}
}
